package com.xiaoxiao.springbootstudy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.source.tree.Tree;
import com.xiaoxiao.springbootstudy.pojo.TreeTest;
import com.xiaoxiao.springbootstudy.service.TreeTestService;
import com.xiaoxiao.springbootstudy.mapper.TreeTestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author XiaoXiao
* @description 针对表【t_tree_test】的数据库操作Service实现
* @createDate 2025-05-19 16:55:17
*/
@Service
public class TreeTestServiceImpl extends ServiceImpl<TreeTestMapper, TreeTest>
    implements TreeTestService{

    @Autowired
    private TreeTestMapper treeTestMapper;

    @Override
    public List<TreeTest> queryTreeListService() {
        return doGetTreeList(treeTestMapper.selectList(null), 0);
    }

    private List<TreeTest> doGetTreeList(List<TreeTest> list, Integer parentId) {
        List<TreeTest> treeList = new ArrayList<>();
        for (TreeTest treeTest : list) {
            if(treeTest.getPid().equals(parentId)) {
                treeList.add(treeTest);
                treeTest.setChildren(doGetTreeList(list, treeTest.getId()));
            }
        }

        return treeList;
    }
}




